home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / misc / xmgr_docs.lha / xmgr_docs / aux / greg2jul.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-02  |  3.1 KB  |  130 lines

  1. /*
  2.  * convert Gregorian days to Julian date
  3.  *
  4.  * Compile with 'cc greg2jul.c -o greg2jul'
  5.  *
  6.  * Modify as needed for your application.
  7.  *
  8.  * The Julian day starts at noon of the Gregorian day and extends
  9.  * to noon the next Gregorian day.
  10.  *
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <math.h>
  15.  
  16. double julday();
  17.  
  18. char *dayofweekstr[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
  19.  
  20. main(argc, argv)
  21.     int argc;
  22.     char **argv;
  23. {
  24.     int cnt, month, day, year, hours = 12, minutes = 0;
  25.     double seconds = 0.0, data;
  26.     char s[256];
  27.  
  28.     while (gets(s) != NULL) {
  29. /* note that seconds is a double */
  30.     sscanf(s, "%d %d %d %d %d %lf %lf", &month, &day, &year, &hours, &minutes, &seconds, &data);
  31.         printf("%lf %lf\n", julday(month, day, year, hours, minutes, seconds), data);
  32.     }
  33. }
  34.  
  35. /*
  36. ** Takes a date, and returns a Julian day. A Julian day is the number of
  37. ** days since some base date  (in the very distant past).
  38. ** Handy for getting date of x number of days after a given Julian date
  39. ** (use jdate to get that from the Gregorian date).
  40. ** Author: Robert G. Tantzen, translator: Nat Howard
  41. ** Translated from the algol original in Collected Algorithms of CACM
  42. ** (This and jdate are algorithm 199).
  43. */
  44. double julday(mon, day, year, h, mi, se)
  45.     int mon, day, year, h, mi;
  46.     double se;
  47. {
  48.     long m = mon, d = day, y = year;
  49.     long c, ya, j;
  50.     double seconds = h * 3600.0 + mi * 60 + se;
  51.  
  52.     if (m > 2)
  53.     m -= 3;
  54.     else {
  55.     m += 9;
  56.     --y;
  57.     }
  58.     c = y / 100L;
  59.     ya = y - (100L * c);
  60.     j = (146097L * c) / 4L + (1461L * ya) / 4L + (153L * m + 2L) / 5L + d + 1721119L;
  61.     if (seconds < 12 * 3600.0) {
  62.     j--;
  63.     seconds += 12.0 * 3600.0;
  64.     }
  65.     else {
  66.     seconds = seconds - 12.0 * 3600.0;
  67.     }
  68.     return (j + (seconds / 3600.0) / 24.0);
  69. }
  70.  
  71. /* Julian date converter. Takes a julian date (the number of days since
  72. ** some distant epoch or other), and returns an int pointer to static space.
  73. ** ip[0] = month;
  74. ** ip[1] = day of month;
  75. ** ip[2] = year (actual year, like 1977, not 77 unless it was  77 a.d.);
  76. ** ip[3] = day of week (0->Sunday to 6->Saturday)
  77. ** These are Gregorian.
  78. ** Copied from Algorithm 199 in Collected algorithms of the CACM
  79. ** Author: Robert G. Tantzen, Translator: Nat Howard
  80. */
  81. calcdate(jd, m, d, y, h, mi, sec)
  82.     double jd;
  83.     int *m, *d, *y, *h, *mi;
  84.     double *sec;
  85. {
  86.     static int ret[4];
  87.  
  88.     long j = jd;
  89.     double tmp, frac = jd - j;
  90.     if (frac >= 0.5) {
  91.     frac = frac - 0.5;
  92.         j++;
  93.     }
  94.     else {
  95.     frac = frac + 0.5;
  96.     }
  97.  
  98.     ret[3] = (j + 1L) % 7L;
  99.     j -= 1721119L;
  100.     *y = (4L * j - 1L) / 146097L;
  101.     j = 4L * j - 1L - 146097L * *y;
  102.     *d = j / 4L;
  103.     j = (4L * *d + 3L) / 1461L;
  104.     *d = 4L * *d + 3L - 1461L * j;
  105.     *d = (*d + 4L) / 4L;
  106.     *m = (5L * *d - 3L) / 153L;
  107.     *d = 5L * *d - 3 - 153L * *m;
  108.     *d = (*d + 5L) / 5L;
  109.     *y = 100L * *y + j;
  110.     if (*m < 10)
  111.     *m += 3;
  112.     else {
  113.     *m -= 9;
  114.     *y++;
  115.     }
  116.     if (*m < 3) *y++;
  117.     tmp = 3600.0 * (frac * 24.0);
  118.     *h = (int) (tmp / 3600.0);
  119.     tmp = tmp - *h * 3600.0;
  120.     *mi = (int) (tmp / 60.0);
  121.     *sec = tmp - *mi * 60.0;
  122. }
  123.  
  124. int dayofweek(j)
  125.     double j;
  126. {
  127.     j += 0.5;
  128.     return (int) (j + 1) % 7;
  129. }
  130.